import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'
import app from '../App.vue'
import index from '../views/index.vue'
import login from '../views/login.vue'
import register from '../views/register.vue'
import report from '../views/report.vue'
import personal from '../views/personal.vue'
import hospital from '../views/hospital.vue'
import appointment from '../views/appointment.vue'
import appointmentlist from '../views/appointmentlist.vue'
import appointmentsuccess from '../views/appointmentsuccess.vue'
import appointmentcancel from '../views/appointmentcancel.vue'
import setmeal from '../views/setmeal.vue'
import selectdate from '../views/selectdate.vue'
import reportlist from '@/views/reportlist.vue'
import confirmorder from '@/views/confirmorder.vue'


const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/',
    name: 'app',
    component: app
  },
  {
    path: '/index',
    name: 'index',
    component: index
  },
  {
    path: '/login',
    name: 'login',
    component: login,
  },
  {
    path: '/register',
    name: 'register',
    component: register,
  },
  {
    path: '/report',
    name: 'report',
    component: report,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/personal',
    name: 'personal',
    component: personal,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/hospital',
    name: 'hospital',
    component: hospital,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/appointment',
    name: 'appointment',
    component: appointment,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/appointmentlist',
    name: 'appointmentlist',
    component: appointmentlist,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/appointmentsuccess',
    name: 'appointmentsuccess',
    component: appointmentsuccess,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/appointmentcancel',
    name: 'appointmentcancel',
    component: appointmentcancel,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/setmeal',
    name: 'setmeal',
    component: setmeal,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/selectdate',
    name: 'selectdate',
    component: selectdate,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/reportlist',
    name: 'reportlist',
    component: reportlist,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/confirmorder',
    name: 'confirmorder',
    component: confirmorder,
    meta: {
      requireAuth: true
    }
  },

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// router.beforeEach((to, from, next) => {
//   if (to.matched.some(record => record.meta.requiresAuth)) {
//     const userInfo = sessionStorage.getItem('userInfo');

//         if (!userInfo) {
//           alert('请先登录');
//           store.commit('logout');
//           router.push('/login')
//         }


//   } else {
//     // 这个路由不需要登录权限
//     next();
//   }
// });
//在跳转之前：to是当前页面，from是想跳转到的页面，next是实际上到达的页面，若未指定则就会跳转from
router.beforeEach((to, from, next) => {
  if (!to.meta.requireAuth)
    // 判断该路由是否需要登录权限
    next();
  else if (to.meta.requireAuth == true) {
    // 若当前页面需要登录
    if (sessionStorage.getItem('userInfo') == null) {
      // 用户未登录，重定向到登录页
      alert('请先登录！');
      store.commit('logout');
      next('/login');
    } else {
      // 用户已登录，直接访问
      next();
    }
  }
});
export default router
